# -*- coding: utf-8 -*-
"""
Created on Mon Nov 23 14:06:13 2020

@author: 61426
"""

import os
import pandas as pd
from flask import Flask, flash, request, redirect, url_for, escape, jsonify, make_response
from flask_cors import CORS
from flask import render_template
from flask import session

from build_model import lstm_model, add_lstm, svr_model, forecast
from process_data import data_process
from process_data import word_func

app = Flask(__name__)

app.secret_key = '123456'

# 设置跨域
CORS(app, resources=r'/*')


@app.route('/')
def home():
    return render_template('index.html')


@app.route('/index')
def index():
    return render_template('index.html')


# app.debug = True
@app.route('/build/lstm/view')
def build_lstm_view():
    return render_template('build_lstm_view.html')


# 接收lstm页面
@app.route('/build_lstm_data', methods=['POST'])
def build_lstm():
    id = request.form['id']
    time = request.form['time']
    type = request.form['type']
    dir = request.form['dir']
    # TODO 处理数据
    lstm_df = data_process.data_for_LSTM_model(id, time, type, dir)
    print(lstm_df)

    lstm_model.build_lstm(id, time, lstm_df)
    return 'ok'


# 进入建立svr页面
@app.route('/build/svr/view')
def build_svr_view():
    return render_template('build_svr_view.html')


# 接收svr参数
@app.route('/build_svr_data', methods=['POST'])
def build_svr():
    id = request.form['id']
    time = request.form['time']
    type = request.form['type']
    predict_day = request.form['predict_day']
    season = request.form['season']
    ec_dir = request.form['ec_dir']
    ob_dir = request.form['ob_dir']

    # TODO 处理数据
    # 调用改函数 处理ob EC原始数据
    data_process.data_for_SVR_model(id, time, type, ob_dir, ec_dir)

    # 处理季节
    from process_data import obp
    obp.select_ec_merge_by_month(id, time, season, int(predict_day), type)

    add_lstm.add_obp(id, season, int(predict_day), time, type)
    svr_model.build_svr(id, season, int(predict_day), time, type)
    return 'ok'


# 进入预测页面
@app.route('/predict/view')
def predict_view():
    return render_template('forecast_new.html')


# 接收预测数据
@app.route('/predict', methods=['POST'])
def predict():
    id = request.form['id']
    time = request.form['time']
    type = request.form['type']
    predict_day = request.form['predict_day']
    ec_dir = request.form['ec_dir']
    ob_dir = request.form['ob_dir']
    predict_date = request.form['predict_date']

    predictions = []

    season = data_process.data_process_for_predict(id, time, type, predict_date, ob_dir, ec_dir)
    print(season)
    for day in range(1, 1 + int(predict_day)):
        # svr_df, season = data_process.data_for_predict(id, time, type, predict_date, day, ob_dir, ec_dir)
        svr_df = data_process.data_select_for_predict(id, time, type, predict_date, day)
        lstm_path = 'models/lstm' + '/' + time + '/' + id + '_1.h5'
        svr_path = 'models/svr' + '/' + season + '/' + str(
            day) + '天' + '/' + time + '/' + type + '/' + id + '.pkl'
        if not os.path.exists(lstm_path):
            return jsonify('lstm模型为空')
        if not os.path.exists(svr_path):
            return jsonify('svr模型为空')
        prediction = forecast.forecast(id, int(day), time, season, svr_df, type).tolist()[0]
        predictions.append(prediction)
    print(predictions)
    session['id'] = id
    session['feature'] = type
    session['predict_date'] = predict_date
    session['predict_day'] = predict_day
    session['predict_values'] = predictions
    result = {'predictions': predictions}
    return jsonify(result)


# 生成报文
@app.route('/word', methods=['POST'])
def word():
    id = session.get('id')
    predict_date = session.get('predict_date')
    predict_day = session.get('predict_day')
    predict_values = session.get("predict_values")
    feature = session.get("feature")
    print(feature)
    word_func.generate_word_local(id, feature, predict_date, int(predict_day), predict_values)

    return 'ok'


if __name__ == '__main__':
    app.run(threaded=True)
